<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021-04-27
 * Time: 21:27
 */
namespace app\admin\model\traits;
use app\admin\controller\command\NodeAnotation;
use think\Request;
trait Curd
{
    /**
     * @NodeAnotation(title="列表")
     */
    public function index(Request $request){
        if($request->isAjax()){
            $limit = $request->param('limit',10);
            $where = $this->buildTableParames();
            if($this->AllData['is_page']){
                if(empty($where)){
                    $data = $this->model->order('sort desc,id desc')->paginate($limit)->toArray();
                }else{
                    $data = $this->model->where($where)->order('sort desc,id desc')->paginate($limit)->toArray();
                }
                $data['data'] = $this->arrangementData($data['data']);
            }else{
                $where = $this->buildTableParames();
                if(empty($where)){
                    $data = $this->model->order('sort desc,id asc')->select()->toArray();
                }else{
                    $data = $this->model->where($where)->order('sort desc,id asc')->select()->toArray();
                }
                $data = $this->arrangementData($data);
                $res['total'] = count($data);
                $res['data'] = $data;
//            echo '<pre>';
//            print_r($res);
//            exit;
                return json_page_response($res);
            }
            return json_page_response($data);
        }
        $data = $request->param();
        if(isset($data['id'])&&isset($data['field'])&&$data['id']&&$data['field']){
            $parameter = "?".$data['field'].'='.$data['id'];
        }else{
            $parameter = '';
        }
        $this->assign('parameter',$parameter);
//        echo $parameter;
//        exit;
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="添加")
     */
    public function add(Request $request){
        $field = $this->AllData['field'];
        if($request->isAjax()){
            $data = $this->SubmitDataArray();
            if(!(isset($data['create_time'])&&$data['create_time'])){
                $data['create_time'] = time();
            }
            if(!(isset($data['update_time'])&&$data['update_time'])){
                $data['update_time'] = time();
            }
            $bool = $this->model->save($data);
            if($bool){
                return $this->success('添加成功');
            }else{
                return $this->error('添加失败');
            }
        }
        $data = $this->arrangementData();
        $this->assign('data',$data);
        $field = TablHandle::conversionField($field);
        $this->assign('field',$field);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit(Request $request){
        $field = $this->AllData['field'];
        if($request->isAjax()){
            $data = $this->SubmitDataArray();
            if(empty($data['id'])){
                return $this->error('请输入要修改的id');
            }
            if(!(isset($data['update_time'])&&$data['update_time'])){
                $data['update_time'] = time();
            }
            $mo = $this->model->find($data['id']);
            $bool = $mo->save($data);
            if($bool){
                return $this->success('添加成功');
            }else{
                return $this->error('添加失败');
            }
        }
        $tiaojian = $request->param();
        $data = $this->model->find($tiaojian['id'])->toArray();
        foreach ($tiaojian as $key=>$value){
            $data[$key] = $value;
        }
        $this->assign('data',$data);
        $field = TablHandle::conversionField($field);
        $this->assign('field',$field);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="删除")
     */
    public function delete(Request $request){
        $data = $request->param();
        $bool = $this->model->destroy($data['id']);
        if($bool){
            return $this->success('删除成功',$data);
        }else{
            return $this->error('删除失败');
        }
    }
    /**
     * @NodeAnotation(title="导出")
     */
    public function export(){

    }
    /**
     * @NodeAnotation(title="属性修改")
     */
    public function modify(Request $request){
        $data = $request->param();
        $shuju = $this->model->find($data['id']);
        $shujuarray = $shuju->toArray();
        if($shujuarray[$data['field']]==1){
            $bool = $shuju->save([$data['field']=>0,'update_time'=>time()]);
        }else{
            $bool = $shuju->save([$data['field']=>1,'update_time'=>time()]);
        }
        if($bool){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
}